/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.lyqg.action;

import static com.core.Constants.PAGE_PATH_404;
import static com.core.Constants.STATUS_NORMAL;
import static com.lyqg.Constants.SESSION_USER;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.core.freemarker.HtmlPathHelp;
import com.core.orm.Page;
import com.core.orm.PageRequest;
import com.core.utils.WebUtil;
import com.lyqg.biz.IContentService;
import com.lyqg.biz.ICouponService;
import com.lyqg.biz.IFxService;
import com.lyqg.biz.IJyService;
import com.lyqg.biz.IMessageService;
import com.lyqg.biz.IUserService;
import com.lyqg.domain.Collection;
import com.lyqg.domain.Content;
import com.lyqg.domain.Coupon;
import com.lyqg.domain.Fx;
import com.lyqg.domain.Jy;
import com.lyqg.domain.Message;
import com.lyqg.domain.OperatePoint;
import com.lyqg.domain.User;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-7-20 下午11:15:36
 */
@Controller
public class FxAction extends BaseAction {
	@Resource
	IMessageService messageService;
	@Resource
	IFxService fxService;
	@Resource
	IContentService contentService;
	@Resource
	ICouponService couponService;
	@Resource
	IJyService jyService;
	@Resource
	IUserService userService;

	@RequestMapping(value = "/fx/index.html")
	public String index(HttpServletRequest request, ModelMap model) {
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 20);
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		Page<Fx> fxs = null;
		String categoryParentEname = WebUtil.getString(request, "c");
		String mallEname = WebUtil.getString(request, "m");
		if (StringUtils.isNotBlank(categoryParentEname)
				&& StringUtils.isNotBlank(mallEname)) {
			model.addAttribute("c", categoryParentEname);
			model.addAttribute("m", mallEname);
			fxs = fxService.queryFxByMalllAndCate(pageRequest,
					categoryParentEname, mallEname);
		} else if (StringUtils.isNotBlank(categoryParentEname)
				&& StringUtils.isBlank(mallEname)) {
			model.addAttribute("c", categoryParentEname);
			fxs = fxService.queryFxByCate(pageRequest, categoryParentEname);
		} else if (StringUtils.isBlank(categoryParentEname)
				&& StringUtils.isNotBlank(mallEname)) {
			model.addAttribute("m", mallEname);
			fxs = fxService.queryFxByMall(pageRequest, mallEname);
		} else if (StringUtils.isBlank(categoryParentEname)
				&& StringUtils.isBlank(mallEname)) {
			fxs = fxService.queryFx(pageRequest);
		}
		model.addAttribute("fxs", fxs);
		model.addAttribute("currentMenuFx", "currentMenu");
		return HtmlPathHelp.getHtmlPathFront("fx");
	}

	@RequestMapping(value = "/fx/detail.html")
	public String detail(HttpServletRequest request, ModelMap model) {
		int id = WebUtil.getInt(request, "id");
		Fx fx = fxService.getById(id);
		if (fx == null || fx.getStatus() != STATUS_NORMAL) {
			return PAGE_PATH_404;
		}
		model.addAttribute("fx", fx);
		model.addAttribute("collectionType",
				Collection.CollectionType.FX.type());
		PageRequest pageRequest = new PageRequest(1, 10);
		pageRequest.setOrderBy("id");
		pageRequest.setOrderDir(PageRequest.Sort.DESC);
		model.addAttribute(
				"fxs",
				fxService.queryFxByCate(pageRequest,
						fx.getCategoryParentEname()));
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageSize = WebUtil.getInt(request, "pageSize", 5);
		PageRequest pageRequestMessage = new PageRequest(pageNo, pageSize);
		pageRequestMessage.setOrderBy("id");
		pageRequestMessage.setOrderDir(PageRequest.Sort.ASC);
		Page<Message> messages = messageService.queryByContentId(
				pageRequestMessage, Message.MessageType.FX.type(), id);
		model.addAttribute("messages", messages);
		model.addAttribute("currentMenuFx", "currentMenu");
		List<Content> limitContents = contentService.queryLimitContent(12);
		List<Coupon> limitCoupons = couponService.queryLimitCoupons(10);
		List<Jy> limitJys = jyService.queryLimitJy(5);
		model.addAttribute("limitContents", limitContents);
		model.addAttribute("limitCoupons", limitCoupons);
		model.addAttribute("limitJys", limitJys);
		return HtmlPathHelp.getHtmlPathFront("fx_detail");
	}

	@RequestMapping("/fx/add_love_ratings.html")
	@ResponseBody
	public String addLoveRatings(HttpServletRequest request) {
		int love_id = WebUtil.getInt(request, "love_id", 0);
		fxService.updatePingFen(love_id);
		User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
		if (user != null) {
			userService.operatePoint(user.getId(), OperatePoint.LIKE);
		}
		return "";
	}

	@RequestMapping(value = "/fx/go.html")
	public String fxgo(HttpServletRequest request) {
		int id = WebUtil.getInt(request, "id");
		String goUrl = fxService.getGoUrl(id);
		if (StringUtils.isNotBlank(goUrl)) {
			return "redirect:" + goUrl;
		}
		return "redirect:/";
	}
}
